﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using DE.Interfaces;
using MySql.Data.MySqlClient;

namespace MoviesDAL
{
    public class MovieDALHelper:IDALHelper
    {

        MySql.Data.MySqlClient.MySqlConnection con = new MySqlConnection("server=localhost; user id=root; password=ilana1; database=likesus; pooling=false;");

        public IBaseEntityData Create(IBaseEntityData de)
        {
            MySqlDataAdapter myDataAdapter = new MySqlDataAdapter("call CreateMovie(@name,@officialRating,@image)", con);
            myDataAdapter.SelectCommand.Parameters.AddWithValue("@name", ((MovieData)de).MovieName);
            myDataAdapter.SelectCommand.Parameters.AddWithValue("@officialRating", ((MovieData)de).OfficialRating);
            myDataAdapter.SelectCommand.Parameters.AddWithValue("@genre", ((MovieData)de).Genre);
            DataTable myDataSet = new DataTable("movie");
            myDataAdapter.Fill(myDataSet);
            if (myDataSet.Rows.Count != 0)
            {
                ((MovieData)de).MovieID = Convert.ToInt32(myDataSet.Rows[0].ItemArray[0]);
            }
            return de;
        }

        public void Update(IBaseEntityData de) {
            MySqlCommand myCommand = new MySqlCommand("call UpdateMovie(@movieid,@name,@officialRating,@genre)", con);
            myCommand.Parameters.AddWithValue("@movieid", ((MovieData)de).MovieID);
            myCommand.Parameters.AddWithValue("@name", ((MovieData)de).MovieName);
            myCommand.Parameters.AddWithValue("@officialRating", ((MovieData)de).OfficialRating);
            myCommand.Parameters.AddWithValue("@genre", ((MovieData)de).Genre);
            myCommand.ExecuteNonQuery();
        }

        public void Delete(int id) {
            MySqlCommand myCommand = new MySqlCommand("call DeleteMovie(@movieid)", con);
            myCommand.Parameters.AddWithValue("@movieid", (id));
            myCommand.ExecuteNonQuery();
        }

        public IBaseEntityData Fetch(int id, object additions) {
            /*
            MySqlDataAdapter myDataAdapter = new MySqlDataAdapter("call getMovieById(@movieid)", con);
            myDataAdapter.SelectCommand.Parameters.AddWithValue("@movieid", id);
            DataTable myDataSet = new DataTable("movie");
            myDataAdapter.Fill(myDataSet);
            MovieData movie=null;
            if (myDataSet.Rows.Count!=0){
                movie = new MovieData(id, (string)myDataSet.Rows[0].ItemArray[1], (float)myDataSet.Rows[0].ItemArray[2], (string)myDataSet.Rows[0].ItemArray[3]);
            }
            return movie; 
             * 
             */
            return null;
        }

        public int Validate(string one, string two, object additions) {
            return 1; 
        }
    }
}